package face.pay.web.cpn.cpnmachinfo;


import face.pay.cpn.cpnmachinfo.ICpnMachInfoService;
import face.pay.dao.CpnMchInfoDao;
import face.pay.entity.CpnMchInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.DateUtil;
import face.pay.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Controller
@RequestMapping("/cpnmachinfo")
public class CpnMachInfoController {

    @Autowired
    private ICpnMachInfoService iCpnMachInfoService;

    @Autowired
    private CpnMchInfoDao cpnMchInfoDao;

    @RequestMapping("index")
    public String indxe(@CurrentUser UserInfo loginuser, Model model) {
        CpnMchInfo cpnMchInfo = this.iCpnMachInfoService.queryCpnMachInfo(loginuser);
        model.addAttribute("cpnMchInfo", cpnMchInfo);
        return "cpn/CpnMachInfo";
    }

    /**
     * logo上传
     *
     * @param file
     * @param loginUser
     * @return
     */
    @RequestMapping("/logoUpload")
    @ResponseBody
    public ResultMessage fielUpload(@RequestParam("file") MultipartFile file, @CurrentUser UserInfo loginUser) throws Exception {
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.iCpnMachInfoService.logoUpload(fileBytes, originalFilename, loginUser);
    }

    /**
     * 物料上传
     *
     * @param file
     * @param loginUser
     * @return
     */
    @RequestMapping("/materialUpload")
    @ResponseBody
    public ResultMessage materialUpload(@RequestParam("file") MultipartFile file, String keyName, @CurrentUser UserInfo loginUser) throws Exception {
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.iCpnMachInfoService.materialUpload(fileBytes, originalFilename, keyName, loginUser);
    }

    /**
     * 获取物料
     *
     * @param loginUser
     * @param mediaId
     * @return
     */
    @RequestMapping(value = "/getMaterial", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getMaterial(@CurrentUser UserInfo loginUser, @RequestParam String mediaId) {
        return this.iCpnMachInfoService.getMedia(mediaId, loginUser);
    }


    /**
     * 获取卡券开放类目
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("/getapplyprotocol")
    @ResponseBody
    public String getapplyprotocol(@CurrentUser UserInfo loginUser) {
        return this.iCpnMachInfoService.getapplyprotocol(loginUser);
    }


    /**
     * 更新资料
     *
     * @param loginUser
     * @param cpnMchInfo
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultMessage update(@CurrentUser UserInfo loginUser, CpnMchInfo cpnMchInfo) {
        return this.iCpnMachInfoService.update(loginUser, cpnMchInfo);
    }


    /**
     * 提交进件资料
     *
     * @param loginUser
     * @param cpnMchInfo
     * @return
     */
    @RequestMapping("/submit")
    @ResponseBody
    public ResultMessage submit(@CurrentUser UserInfo loginUser, CpnMchInfo cpnMchInfo) {
        //更新数据库再提交到微信
        this.iCpnMachInfoService.update(loginUser, cpnMchInfo);
        return this.iCpnMachInfoService.submit(loginUser, cpnMchInfo);
    }


    /**
     * 进件失败修改后提交进件资料
     *
     * @param loginUser
     * @param cpnMchInfo
     * @return
     */
    @RequestMapping("/modifySubmit")
    @ResponseBody
    public ResultMessage modifySubmit(@CurrentUser UserInfo loginUser, CpnMchInfo cpnMchInfo) {
        //更新数据库再提交到微信
        String end_date = cpnMchInfo.getEnd_date();
        String pattern = "yyyy年MM月dd日";
        Date date = DateUtil.dateParse(end_date, pattern);
        cpnMchInfo.setEnd_time(date.getTime() / 1000);
        cpnMchInfo.setUpdatedBy(loginUser.getId());
        this.cpnMchInfoDao.update(cpnMchInfo);
        return this.iCpnMachInfoService.modifySubmit(loginUser, cpnMchInfo);
    }

}
